# -*- coding: utf-8 -*-
import os
import re
import hashlib
class Mvninstall():
  def main(self):
    print 'input groupId (e:org.argszero):'
    group_id=raw_input()
    print 'input version (e:0.0.1):'
    version=raw_input()
    print 'groupId is %(group_id)s and version is %(version)s'%{'group_id':group_id,'version':version}
    print 'input includes (e:.*jar$):'
    includes=raw_input()
    print 'input excludes (e:.*dist.*,.*target.*):'
    excludes = raw_input()
    with open('cmd.bat','w') as cmd_f:
      with open('dependencies.xml','w') as dp_f:
        dp_f.write('<dependencies>')
        for root,dirs,files in os.walk(os.getcwd()):
          for file in filter(lambda f:reduce(lambda x,y:x or re.match(y,f),includes.split(','),False) and not reduce(lambda x,y:x or re.match(y,f),excludes.split(','),False) and not self.check_exists_md5(f),map(lambda f:'%(root)s%(sep)s%(file)s'%{'root':root,'file':f,'sep':os.sep},files)):
            dict={'file_path':file,'group_id':group_id,'artifact_id':os.path.basename(os.path.splitext(file)[0]),'version':version}
            cmd='cmd.exe /c mvn install:install-file -Dfile="%(file_path)s" -DgroupId=%(group_id)s -DartifactId=%(artifact_id)s -Dversion=%(version)s -Dpackaging=jar  -DgeneratePom=true \n'%dict
            cmd_f.write(cmd)
            dp= '''
  <dependency>
    <groupId>%(group_id)s</groupId>
    <artifactId>%(artifact_id)s</artifactId>
    <version>%(version)s</version>
  </dependency>
    '''%dict
            dp_f.write(dp)
        dp_f.write('</dependencies>')

  def check_exists_md5(self,f):
    md5= self.get_cmd5(f)
    self.md5s = hasattr(self,'md5s') and self.md5s or []
    if md5 in self.md5s:
      return True
    else:
      self.md5s.append(md5)
      return False
  def get_cmd5(self,f):      
    m = hashlib.md5()
    with open(f,'r') as f:
      while True:
        d = f.read(1024)
        if not d:
          break
        m.update(d)
    return m.hexdigest()   
if __name__=='__main__':
  Mvninstall().main()
#  m = Mvninstall()
#  print m.check_exists_md5(r'd:\greensoft\cmd\test.bat')
#  print m.check_exists_md5(r'd:\greensoft\cmd\test.bat')
